import json
import requests
from bs4 import BeautifulSoup
from utils.convert import header_placeholder, add_ad_service, footer_placeholder

# 新样式
protein_style = """
.cxsw_description h3 {
    color: #2c5282;
    border-bottom: 2px solid #4299e1;
    padding-bottom: 8px;
    margin-top: 25px;
    font-size: 24px;
}

.cxsw_description h4 {
    color: #4a5568;
    margin-top: 15px;
    font-size: 22px;
}

.cxsw_description p {
    margin: 12px 0;
}

.cxsw_description table {
    width: 100%;
    border-collapse: collapse;
    margin: 15px 0;
    background-color: white;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    border-radius: 4px;
    overflow: hidden;
}

.cxsw_description th,
td {
    padding: 12px 15px;
    text-align: left;
    border-bottom: 1px solid #e2e8f0;
}

.cxsw_description th {
    background-color: #edf2f7;
    font-weight: 600;
    color: #2d3748;
}

.cxsw_description tr:nth-child(even) {
    background-color: #f7fafc;
}

.cxsw_description tr:hover {
    background-color: #ebf4ff;
}

.product-info-item {
    background-color: white;
    border-radius: 6px;
    padding: 15px 20px;
    margin-bottom: 10px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.cxsw_description ol {
    padding-left: 20px;
}

.cxsw_description ol li {
    margin-bottom: 8px;
}

.cxsw_description a {
    color: #3182ce;
    text-decoration: none;
}

.cxsw_description a:hover {
    text-decoration: underline;
}

.cxsw_description #species-reactivity {
    font-weight: 500;
    color: #4a5568;
}

.cxsw_description .product-header {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
}

.cxsw_description .product-code {
    font-size: 22px;
    font-weight: bold;
    color: #2c5282;
    margin-right: 15px;
}
"""


def process_des(html: str):
    # 清除空 img 标签
    # content = html.replace('<img src=""/>\n', "")
    soup = BeautifulSoup(html, "html.parser")

    # # 删除data:image 的img 标签
    # img_tag = soup.find("img", src=lambda x: x and x.startswith("data:image"))
    # if img_tag:
    #     img_tag.decompose()

    # # 删除旧版广告
    # old_ad_tag = soup.find("div", attrs={"id": "con_header"})
    # if old_ad_tag:
    #     old_ad_tag.decompose()
    # 创建新 html
    _description = BeautifulSoup("", "html.parser")
    div_dom = _description.new_tag("div", attrs={"class": "cxsw_description"})

    # 创建样式标签
    style_tag = soup.new_tag("style")
    style_tag.string = protein_style
    div_dom.append(style_tag)
    div_dom.append(soup)

    return add_ad_service() + header_placeholder() + str(div_dom) + footer_placeholder()
